package net.lab1024.smartadmin.module.business.atmosphere.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.atmosphere.domain.dto.AtmosphereAddDTO;
import net.lab1024.smartadmin.module.business.atmosphere.domain.dto.AtmosphereQueryDTO;
import net.lab1024.smartadmin.module.business.atmosphere.domain.dto.AtmosphereUpdateDTO;
import net.lab1024.smartadmin.module.business.atmosphere.domain.entity.AtmosphereEntity;
import net.lab1024.smartadmin.module.business.atmosphere.domain.vo.AtmosphereExcelVO;
import net.lab1024.smartadmin.module.business.atmosphere.domain.vo.AtmosphereVO;
import net.lab1024.smartadmin.module.business.atmosphere.service.AtmosphereService;
import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * [ ���򻷾� ]
 *
 * @author wuhq
 * @version 1.0
 * @company ��������
 * @copyright (c)  ��������Inc. All rights reserved.
 * @date 2021-11-19 11:22:46
 * @since JDK1.8
 */
@RestController
@Api(tags = {"���򻷾�"})
public class AtmosphereController extends BaseController {

    @Autowired
    private AtmosphereService atmosphereService;

    @ApiOperation(value = "����id��ȡ����",notes = "@author �Ζ�")
    @GetMapping("/atmosphere/getById/{id}")
    public ResponseDTO<AtmosphereEntity> getById(@PathVariable Long id) {
        return ResponseDTO.succData(atmosphereService.getById(id));
    }

    @ApiOperation(value = "��ҳ��ѯ���򻷾�",notes = "@author wuhq")
    @PostMapping("/atmosphere/page/query")
    public ResponseDTO<PageResultDTO<AtmosphereVO>> queryByPage(@RequestBody AtmosphereQueryDTO queryDTO) {
        if(queryDTO.getCreateUser()!=null){
            queryDTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        }
        queryDTO.setIsIndexShow(1);
        queryDTO.setMyUserId(SmartRequestTokenUtil.getRequestUserId());
        return atmosphereService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "������򻷾�",notes = "@author wuhq")
    @PostMapping("/atmosphere/add")
    public ResponseDTO<String> add(@RequestBody @Validated AtmosphereAddDTO addTO){
        addTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setCreateTime(new Date());
        return atmosphereService.add(addTO);
    }

    @ApiOperation(value="�޸����򻷾�",notes = "@author wuhq")
    @PostMapping("/atmosphere/update")
    public ResponseDTO<String> update(@RequestBody @Validated AtmosphereUpdateDTO updateDTO){
        return atmosphereService.update(updateDTO);
    }

    @ApiOperation(value="����ɾ�����򻷾�",notes = "@author wuhq")
    @PostMapping("/atmosphere/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return atmosphereService.deleteByIds(idList);
    }

    @ApiOperation(value = "��������", notes = "@author wuhq")
    @PostMapping("/atmosphere/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //��ѯ����
        List<AtmosphereExcelVO> atmosphereList = atmosphereService.queryBatchExportData(idList);
        //��������
        ExportParams ex = new ExportParams("���򻷾�", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, AtmosphereExcelVO.class, atmosphereList);
        downloadExcel("���򻷾�", workbook, response);
    }

    @ApiOperation(value = "����ȫ��", notes = "@author wuhq")
    @PostMapping("/atmosphere/export/all")
    public void exportAll(@RequestBody @Validated AtmosphereQueryDTO queryDTO, HttpServletResponse response) {
        //��ѯ����
        List<AtmosphereExcelVO> atmosphereList = atmosphereService.queryAllExportData(queryDTO);
        //��������
        ExportParams ex = new ExportParams("���򻷾�", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, AtmosphereExcelVO.class, atmosphereList);
        downloadExcel("���򻷾�", workbook, response);
    }

}
